<?php
/*
Primary Developer: Derek Shue
Secondary: Alex Arey
Updated: 12/10/2011
Module Description: VolunteerQuery provides an interface for volunteer-related database query actions
*/
class Application_Model_VolunteerQuery
{
    public function init(){
    //init does nothing
	}

    public function getVRs_CP($partnerID){
	$term = new Application_Model_Term();
        $current_term = $term->getCurrentOpenTermStr('cpvol');
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		$query = 'TERM = \'' . $current_term . '\' and CPID = ' . $partnerID;
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
	return $rows;
    }
	public function getVRsByTerm_CP($partnerID, $term){
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		$query = 'TERM = \'' . $term . '\' and CPID = ' . $partnerID;
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
	return $rows;
    }
	
	public function getVRs_Admin(){
		$term = new Application_Model_Term();
        $current_terms = $term->getCurrentFutureTermsStr_admin();
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		
		$query = 'TERM in ' . $current_terms;
		
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			echo $e->getMessage();
			$rows = Null;
		}
		return $rows;
	}
	public function getVRsByTerm_Admin($term){
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
	
		$query = 'TERM =\'' . $term . '\'';
	
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			echo $e->getMessage();
			$rows = Null;
		}
		return $rows;
	}
	
	
	
    public function selectVRs_CP($partnerID, $term, $termModifier, $name){
		$terms = new Application_Model_Term();
		try{
		   $term_list = $terms->getTerms($term, $termModifier);
		}
		catch(Exception $e){
			return Null;
		}
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		$query = 'TERM in ' . $term_list . ' and CPID = ' . $partnerID . ' and upper(POSITIONTITLE) like \'%'.strtoupper($name).'%\'';
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
    }
	
	
	public function selectVRs_Admin($term, $termModifier, $name){
		$terms = new Application_Model_Term();
		try{
		   $term_list = $terms->getTerms($term, $termModifier);
		}
		catch(Exception $e){
			return Null;
		}
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		$query = 'TERM in ' . $term_list . ' and upper(POSITIONTITLE) like \'%'.strtoupper($name).'%\'';
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
    }

    public function selectVR_ID($vrid){
		$positionModel = new Application_Model_DbTable_VolunteerRequests();
		$query = 'VRID =  ' . $vrid;
		try{
			$rows = $positionModel->fetchAll($query)->toArray();
		}
		catch(Exception $e){
			$rows = Null;
		}
		return $rows;
	}

	public function getPartnerNameForVRID($vrid){
		$volModel = new Application_Model_DbTable_VolunteerRequests();
		$cpModel = new Application_Model_DbTable_Organizations();
		try{
			$vrow = $volModel->getVolunteerRequest('VRID = '.$vrid);
			$cpid = $vrow['CPID'];
			$prow = $cpModel->getOrganization('ID = '.$cpid);
		}
		catch(Exception $e){
			$prow = Null;
		}
		return $prow['NAME'];
	}
	
}
?>
